package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.AttendPunchDailyReportAddDTO;
import org.jsola.hr.dto.AttendPunchDailyReportUpdateDTO;
import org.jsola.hr.query.AttendPunchDailyReportQuery;
import org.jsola.hr.query.AttendPunchRecordSimpleQuery;
import org.jsola.hr.query.AttendRecordStatQuery;
import org.jsola.hr.query.AttendStatusQuery;
import org.jsola.hr.service.IAttendPunchDailyReportService;
import org.jsola.hr.vo.AttendDailyPeriodVO;
import org.jsola.hr.vo.AttendPunchDailyReportListVO;
import org.jsola.hr.vo.AttendPunchDailyReportVO;
import org.jsola.hr.vo.AttendPunchRecordSimpleVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * @author zhr
 */
@RestController("hrAttendPunchDailyReportController")
@RequestMapping(HrConstants.API_PATH + "/v1/attendPunchDailyReports")
public class AttendPunchDailyReportController implements IAttendPunchDailyReportControllerAPI {

    @Autowired
    private IAttendPunchDailyReportService attendPunchDailyReportService;

    @Override
    @PostMapping
    public Result<AttendPunchDailyReportVO> save(@Valid @RequestBody AttendPunchDailyReportAddDTO attendPunchDailyReportAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增考勤打卡日报表权限校验

        return Result.success(
                attendPunchDailyReportService.save(attendPunchDailyReportAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody AttendPunchDailyReportUpdateDTO attendPunchDailyReportUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改考勤打卡日报表权限校验

        int count = attendPunchDailyReportService.update(attendPunchDailyReportUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{attendPunchDailyReportId}")
    public Result<Integer> delete(@PathVariable String attendPunchDailyReportId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除考勤打卡日报表权限校验

        int count = attendPunchDailyReportService.deleteByIds(tokenUser, attendPunchDailyReportId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] attendPunchDailyReportIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除考勤打卡日报表权限校验

        int count = attendPunchDailyReportService.deleteByIds(tokenUser, attendPunchDailyReportIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{attendPunchDailyReportId}")
    public Result<AttendPunchDailyReportVO> findById(@PathVariable String attendPunchDailyReportId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看考勤打卡日报表权限校验

        return Result.success(
            attendPunchDailyReportService.selectById(attendPunchDailyReportId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<AttendPunchDailyReportListVO>> listAll(@Valid AttendPunchDailyReportQuery attendPunchDailyReportQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看考勤打卡日报表权限校验

        return Result.success(
            attendPunchDailyReportService.select(attendPunchDailyReportQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<AttendPunchDailyReportListVO>> page(@Valid AttendRecordStatQuery attendRecordStatQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            attendPunchDailyReportService.selectPage(attendRecordStatQuery, tokenUser.getSiteId())
        );
    }

    /**
     * 异常打卡详情
     *
     * @param attendStatusQuery
     * @return
     */
    @Override
    @GetMapping(value = "selectAttendStatus")
    public Result<List<AttendDailyPeriodVO>> selectAttendStatus(AttendStatusQuery attendStatusQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                attendPunchDailyReportService.selectAttendStatus(attendStatusQuery, tokenUser.getSiteId())
        );
    }

    /**
     * 查看考勤记录
     *
     * @param attendPunchRecordSimpleQuery
     * @return
     */
    @Override
    @GetMapping(value = "findSimpleRecordList")
    public Result<List<AttendPunchRecordSimpleVO>> findSimpleRecordList(AttendPunchRecordSimpleQuery attendPunchRecordSimpleQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                attendPunchDailyReportService.selectSimpleRecordList(attendPunchRecordSimpleQuery, tokenUser.getSiteId())
        );
    }


}

